package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmFinanceInfo;
import com.nuoniu.sibanyun.entity.crm.CrmRequirement;
import com.nuoniu.sibanyun.service.crm.ICrmRequirementService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 需求管理 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/crm/requirement")
@Api(tags = "客户需求管理")
public class CrmRequirementController extends BaseController {

    @Autowired
    private ICrmRequirementService requirementService;

    /**
     * 分页获取需求
     * @author 小啊刚
     * @date 2020/7/9
     */
    @GetMapping("page")
    public HttpResult pageRequirement(Page page, CrmRequirement requirement) {
        return HttpResult.ok(requirementService.pageRequirement(page, requirement));
    }

    /**
     * 保存需求
     * @author 小啊刚
     * @date 2020/7/9
     */
    @PostMapping("save")
    public HttpResult saveRequirement(@RequestBody CrmRequirement requirement) {
        UserInfo userInfo = UserThreadLocal.get();
        if(requirement.getId() == null) {
            requirement.setCreateUserId(userInfo.getUserId());
            requirement.setCreateUserName(userInfo.getRealName());
        }else {
            requirement.setUpdateDate(new Date());
            requirement.setUpdateUserId(userInfo.getUserId());
            requirement.setUpdateUserName(userInfo.getRealName());
        }
        requirementService.saveOrUpdate(requirement);
        return HttpResult.ok();
    }

    /**
     * 停用需求
     * @author 小啊刚
     * @date 2020/7/9
     */
    @PostMapping(value = "disable")
    public HttpResult disableSelected(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            CrmRequirement requirement = new CrmRequirement();
            requirement.setId(id);
            requirement.setStatus(2);
            requirementService.updateById(requirement);
        }
        return HttpResult.ok("停用需求成功");
    }
}
